package com.groupbuying.store.mq;

import com.alibaba.fastjson.JSON;
import com.groupbuying.store.domain.StoreMainInfoDO;
import com.groupbuying.store.pojo.StoreMainInfoEnum;
import com.groupbuying.store.service.StoreMainInfoService;
import com.groupbuying.store.utils.DateFormatHelper;
import com.groupbuying.store.utils.JSONUtils;
import com.groupbuying.store.utils.apiresult.ApiResultCode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @anthor wenguoqiang
 * @date: 2019/06/10 9:24
 * @company 够样
 * @des
 **/
@Component
public class StoreReportResult {

    public static final Logger LOGGER = LoggerFactory.getLogger(StoreReportResult.class);

    @Autowired
    private StoreMainInfoService storeMainInfoService;

    public boolean dealAsyResult(Map<String, Object> result) {
        LOGGER.info("StoreReportResult.dealAsyResult()------>获取银盛报备异步回执信息map："+result.toString());
        //注册的商户号
        String usercode = result.get("usercode").toString();
        //客户状态  生效 审核失败  这里银盛接口有错单词 查询是status 异步通知接口是 statue
        String custStatus = result.containsKey("cust_statue")?result.get("cust_statue").toString():result.get("cust_status").toString();
        //备注
        String note = result.get("note").toString();
        //平台标识
        String merchantNo = result.get("merchant_no").toString();
        //用户状态
        String userStatus = result.get("user_status").toString();

        StoreMainInfoDO storeMainInfo = storeMainInfoService.get(merchantNo);
        LOGGER.info("StoreReportResult.dealAsyResult()------>获取storeMainInfo："+storeMainInfo.toString());
        if (storeMainInfo == null) {
            LOGGER.error("Json信息：{}" , JSONUtils.beanToJson(result));
            LOGGER.error("商户信息不存在：storeUuid:{}" , merchantNo);
            return false;
        }
        storeMainInfo.setOpetime(DateFormatHelper.getNowTimeStr());
        if ("生效".equals(userStatus)) {
            //生效
            storeMainInfo.setYsuserstate("2");
            storeMainInfo.setStorenum(merchantNo);
            storeMainInfo.setYsusercode(usercode);
            storeMainInfo.setYsnote(custStatus);
            //Todo: 财务审核功能
            storeMainInfo.setAuditstatus(StoreMainInfoEnum.BUSINESS_AUDIT_PASS);
        } else {
            //修改注册状态为未注册 商户可以二次注册
            storeMainInfo.setYsuserstate("0");
            storeMainInfo.setYsnote(String.format("银盛异步通知审核失败信息如下 1.%s",note ));
            storeMainInfo.setYsusercode(usercode);
            storeMainInfo.setAuditstatus(StoreMainInfoEnum.REPORT_UNDONE);
            //storeMainInfo.setNotpassreason(note);
        }
        storeMainInfoService.update(storeMainInfo);

        return true;
    }

    public boolean dealSynResult(Map<String, Object> registryResultMap) {
        LOGGER.info("StoreReportResult.dealSynResult()------>获取银盛报备同步回执信息map："+registryResultMap.toString());
        String msg = registryResultMap.get("msg").toString();
        LOGGER.info("StoreReportResult.dealSynResult()------>msg："+msg);
        Map<String, Object> map = JSON.parseObject(registryResultMap.get("result").toString(), Map.class);
        LOGGER.info("StoreReportResult.dealSynResult()------>map："+map.toString());
        String merchantNo = map.get("merchant_no").toString();
        LOGGER.info("StoreReportResult.dealSynResult()------>merchantNo："+merchantNo);
        String errorMsg = map.containsKey(msg)? map.get("msg").toString():"";
        StoreMainInfoDO storeMainInfo = storeMainInfoService.get(merchantNo);
        LOGGER.info("StoreReportResult.dealSynResult()------>获取storeMainInfo："+storeMainInfo.toString());
        if (storeMainInfo != null) {
            //商户信息已被报备
            if ("2".equals(storeMainInfo.getYsuserstate())) {
                return false;
            }
            if(map.containsKey("usercode")){
                String usercode = map.get("usercode").toString();
                storeMainInfo.setYsusercode(usercode);
            }
            if((!"报备成功".equals(msg))|| Integer.parseInt(registryResultMap.get("code").toString()) == ApiResultCode.OPRFAILURE.code()){
                storeMainInfo.setAuditstatus(StoreMainInfoEnum.REPORT_UNDONE);
                //storeMainInfo.setNotpassreason(msg);
                storeMainInfo.setYsnote(String.format("银盛报备返回失败信息如下 1.%s %s",msg,errorMsg));
                storeMainInfo.setOpetime(DateFormatHelper.getNowTimeStr());
            }
            if(map.containsKey("userStatus")){
                String userStatus = map.get("userStatus").toString();
                if (Integer.parseInt(registryResultMap.get("code").toString()) == 1 && "生效".equals(userStatus)) {

                    storeMainInfo.setOpetime(DateFormatHelper.getNowTimeStr());
                    storeMainInfo.setStorenum(merchantNo);
                    //银盛中商户注册状态 0 未注册  1 已注册待审核 2审核通过
                    // 非企业商户走系统自动审核，企业商户需要人工审核
                    storeMainInfo.setYsuserstate("2");
                    //Todo:财务审核问题
                    storeMainInfo.setAuditstatus(StoreMainInfoEnum.BUSINESS_AUDIT_PASS);
                }
            }
            storeMainInfoService.update(storeMainInfo);
            return true;
        }else{
            LOGGER.error("Json信息：" + JSONUtils.beanToJson(map));
            LOGGER.error("商户信息不存在：storeUuid:" + merchantNo);
        }
        return false;
    }

    public boolean dealQueryResult(Map<String, Object> result) {
        return dealAsyResult(result);
    }

    public boolean dealErrorResult(Map<String, Object> result){
        LOGGER.info("StoreReportResult.dealSynResult()------>获取银盛报备同步回执信息map：{}",JSONUtils.beanToJson(result));
        String msg = result.get("msg").toString();
        LOGGER.info("StoreReportResult.dealSynResult()------>msg：{}",msg);
        String merchantNo = result.get("merchant_no").toString();
        LOGGER.info("StoreReportResult.dealSynResult()------>merchantNo：{}",merchantNo);
        StoreMainInfoDO storeMainInfo = storeMainInfoService.get(merchantNo);
        LOGGER.info("StoreReportResult.dealSynResult()------>获取storeMainInfo{}",storeMainInfo.toString());
        if (storeMainInfo != null) {

                storeMainInfo.setYsnote(String.format("访问银盛报备失败 1.%s",msg ));
                storeMainInfo.setOpetime(DateFormatHelper.getNowTimeStr());
                storeMainInfo.setAuditstatus(StoreMainInfoEnum.REPORT_UNDONE);


            storeMainInfoService.update(storeMainInfo);
            return true;
        }else{
            LOGGER.error("商户信息不存在：storeUuid:{}", merchantNo);
        }
        return false;
    }

}
